package com.ctshk.sapp.order.custom.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.rpc.finance.req.supplier.AprovalReq;
import com.ctshk.rpc.order.custom.dto.order.ApprovalRefundListDTO;
import com.ctshk.rpc.order.custom.dto.order.CustomOrderTouristRefundAddCalculationDTO;
import com.ctshk.rpc.order.custom.dto.order.CustomOrderTouristRefundListDTO;
import com.ctshk.rpc.order.custom.dto.order.TouristRefundDetailListDTO;
import com.ctshk.rpc.order.custom.dto.tourist.CustomOrderTouristInfoListCollectionDTO;
import com.ctshk.rpc.order.custom.req.order.*;
import com.ctshk.rpc.order.custom.req.schedule.TripScheduleInfoReq;
import com.ctshk.rpc.order.custom.req.tourist.CustomOrderTouristIdReq;
import com.ctshk.rpc.order.custom.service.ICustomizedOrderTouristRefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.ctshk.common.web.support.BaseController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 游客退款 前端控制器
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-02-22
 */
@Api(value="游客退款", tags="游客退款")
@RestController
@Slf4j
@RequestMapping("/customized-order-tourist-refund")
public class CustomizedOrderTouristRefundController extends BaseController {

    @DubboReference
    private ICustomizedOrderTouristRefundService orderTouristRefundService;

    @ApiOperation(value = "列表", notes = "退款记录列表（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "退款记录列表", operDesc = "退款记录列表")
    @PostMapping("/list")
    public Result<PageResponse<CustomOrderTouristRefundListDTO>> list(@RequestBody @Valid CustomOrderTouristRefundListReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return Result.success(orderTouristRefundService.list(req));
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="团期详情-退款明细", notes="团期详情-退款明细(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "团期详情-退款明细", operDesc = "团期详情-退款明细")
    @PostMapping("/schedule-refund-list")
    public Result<List<CustomOrderTouristRefundListDTO>> scheduleRefundList(@RequestBody @Valid TripScheduleInfoReq req) {
        try {
            List<CustomOrderTouristRefundListDTO> dto = orderTouristRefundService.scheduleRefundList(req);
            return Result.success(dto);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "退款", notes = "退款（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "退款管理-退款", operDesc = "退款管理-退款")
    @PostMapping("/add")
    public Result add(@RequestBody CustomOrderTouristRefundAddReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderTouristRefundService.add(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="退款明细", notes="退款管理-退款明细(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "团期详情-退款明细", operDesc = "团期详情-退款明细")
    @PostMapping("/refund-list")
    public Result<List<TouristRefundDetailListDTO>> refundList(@RequestBody @Valid CustomOrderTouristIdReq req) {
        try {
            List<TouristRefundDetailListDTO> dto = orderTouristRefundService.refundList(req);
            return Result.success(dto);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value="撤回", notes="撤回(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "团期详情-退款明细", operDesc = "团期详情-退款明细")
    @PostMapping("/withdraw")
    public Result withdraw(@RequestBody @Valid CustomOrderRefundWithdrawReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderTouristRefundService.withdraw(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "添加计算", notes = "添加计算（退款管理-退款）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "退團管理-申請退團", operDesc = "退團管理-申請退團")
    @PostMapping("/add/calculation")
    public Result<CustomOrderTouristRefundAddCalculationDTO> addCalculation(@RequestBody CustomOrderTouristRefundAddCalculationReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderTouristRefundService.addCalculation(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "操作退款", notes = "操作退款（退款管理-操作退款）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "退款管理-操作退款", operDesc = "退款管理-操作退款")
    @PostMapping("/confirm")
    public Result confirm(@RequestBody @Valid CustomOrderRefundWithdrawReq req) {
        try {
            return orderTouristRefundService.confirm(req, getCurrentUser(getRequest()));
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "审批", notes = "审批(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "退款管理-审批", operDesc = "退款管理-审批")
    @PostMapping("/approval")
    public Result approval(@RequestBody @Valid RefundApprovalReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderTouristRefundService.approval(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "审批中心-退款信息列表", notes = "审批中心-退款信息列表(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "审批中心-退款信息列表", operDesc = "审批中心-退款信息列表")
    @PostMapping("/approval-refund-list")
    public Result<List<ApprovalRefundListDTO>> approvalRefundList(@RequestBody @Valid ApprovalIdReq req) {
        try {
            return orderTouristRefundService.approvalRefundList(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

}
